package com.ellricky.newtv

import android.app.Activity
import android.content.Context
import android.os.Bundle
import androidx.multidex.MultiDex
import androidx.multidex.MultiDexApplication
import com.ellricky.baselib.util.EAppUtil
import com.tencent.bugly.crashreport.CrashReport
import me.jessyan.autosize.AutoSizeConfig

class NewTvApp : MultiDexApplication() {

    private val activityList = arrayListOf<Activity>()

    //单例化的第一种方式：声明一个简单的Application属性
    companion object {
        //情况一：声明可空的属性
        private var instance: NewTvApp? = null
        fun instance() = instance!!
    }

    override fun onCreate() {
        super.onCreate()
        instance = this
        EAppUtil.initApp(this)
        AutoSizeConfig.getInstance().isBaseOnWidth = false
        CrashReport.initCrashReport(applicationContext, "492a92c7d3", false)
        registerActivityLifecycleCallbacks(object : ActivityLifecycleCallbacks {
            override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
                activityList.add(activity)
            }

            override fun onActivityStarted(activity: Activity) {

            }

            override fun onActivityResumed(activity: Activity) {

            }

            override fun onActivityPaused(activity: Activity) {

            }

            override fun onActivityStopped(activity: Activity) {

            }

            override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {

            }

            override fun onActivityDestroyed(activity: Activity) {
                activityList.remove(activity)
            }
        })
    }

    override fun attachBaseContext(base: Context?) {
        super.attachBaseContext(base)
        MultiDex.install(base)
    }

    fun exit() {
        activityList.forEach {
            it.finish()
        }
    }
}